﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Net;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.Linq;

namespace G.APITest.Config
{
    [DataContract]
    public class APIProjectConfig : G.SL.Data.NotifyObject
    {
        public APIProjectConfig()
        {
            this.APIGroups = new ObservableCollection<APIGroup>();
            this.GlobalHeaders = new ObservableCollection<KVPair>();
            this.GlobalParams = new ObservableCollection<KVPair>();
        }

        private Part.IAPIRequestPart apiRequestPart;
        [IgnoreDataMember]
        [Import(typeof(G.APITest.Part.IAPIRequestPart))]
        public Part.IAPIRequestPart APIRequestPart
        {
            get
            {
                return apiRequestPart;
            }
            set
            {
                apiRequestPart = value;
                if (apiRequestPart.ExtensionProjectProperties == null)
                    ExtensionProjectProperties = null;
                else if (extensionProjectProperties == null)
                    ExtensionProjectProperties = apiRequestPart.ExtensionProjectProperties;
                else
                {
                    ExtensionProjectProperties = (from c in apiRequestPart.ExtensionProjectProperties select new Config.KVPair() { Key = c.Key, Value = (extensionProjectProperties.FirstOrDefault(a => a.Key == c.Key) == null ? c.Value : extensionProjectProperties.FirstOrDefault(a => a.Key == c.Key).Value) }).ToArray();
                }
                NotifyPropertyChanged("APIRequestPart");
            }
        }

        private Config.KVPair[] extensionProjectProperties;
        [DataMember]
        public Config.KVPair[] ExtensionProjectProperties
        {
            get
            {
                return extensionProjectProperties;
            }
            set
            {
                extensionProjectProperties = value;
                this.NotifyPropertyChanged("ExtensionProjectProperties");
            }
        }

        [DataMember]
        public string APIRequestPartPath { get; set; }

        [DataMember(Name = "ProjectName")]
        public string ProjectName { get; set; }

        //bool isOauthSign;
        ///// <summary>
        ///// 是否是Oauth签名
        ///// </summary>
        //[DataMember(Name = "IsOauthSign")]
        //public bool IsOauthSign
        //{
        //    get
        //    {
        //        return isOauthSign;
        //    }
        //    set
        //    {
        //        if (isOauthSign != value)
        //        {
        //            isOauthSign = value;
        //            NotifyPropertyChanged("IsOauthSign");
        //        }
        //    }
        //}
        #region oauth
        /// <summary>
        /// Oauth签名版本
        /// </summary>
        //[XmlElement("OuthVersion")]
        //[DataMember(Name = "OuthVersion")]
        //public double OuthVersion { get; set; }

        //[XmlElement("APPKey")]
        //[DataMember(Name = "APPKey")]
        //public string APPKey { get; set; }

        //[XmlElement("APPSecret")]
        //[DataMember(Name = "APPSecret")]
        //public string APPSecret { get; set; }
        #region oauth2.0

        ///// <summary>
        ///// 请求验证的地址
        ///// </summary>
        //[XmlElement("Request_Authorize_Url")]
        //[DataMember(Name = "Request_Authorize_Url")]
        //public string Request_Authorize_Url { get; set; }
        ///// <summary>
        ///// 获取RequestToken是地址
        ///// </summary>
        //[XmlElement("Token_Authorize_Url")]
        //[DataMember(Name = "Token_Authorize_Url")]
        //public string Token_Authorize_Url { get; set; }
        //[XmlElement("ClientID")]
        //public string ClientID { get; set; }
        //[XmlElement("ClientSecret")]
        //public string ClientSecret { get; set; }

        //private string authorize_ResponseType = "code";
        /// <summary>
        /// 验证的类型，默认为code
        /// </summary>
        //[XmlElement("Authorize_ResponseType")]
        //[DataMember(Name = "Authorize_ResponseType")]
        //public string Authorize_ResponseType
        //{
        //    get
        //    {
        //        return authorize_ResponseType;
        //    }
        //    set
        //    {
        //        authorize_ResponseType = value;
        //    }
        //}
        /// <summary>
        /// 验证回掉地址
        /// </summary>
        //[XmlElement("RedirectUri")]
        //[DataMember(Name = "RedirectUri")]
        //public string RedirectUri { get; set; }
        #endregion
        #endregion

        [DataMember(Name = "APIBaseUri")]
        public string APIBaseUri { get; set; }

        [DataMember(Name = "GlobalHeaders")]
        public ObservableCollection<KVPair> GlobalHeaders { get; set; }

        [DataMember(Name = "GlobalParams")]
        //[XmlArrayItem("KVPair")]
        public ObservableCollection<KVPair> GlobalParams { get; set; }

        //[XmlElement("SignName")]
        //public string SignName { get; set; }

        //[XmlElement("SignMethod")]
        //public SignMethd SignMethod { get; set; }

        //private bool isSign;
        //[DataMember(Name = "IsSign")]
        //public bool IsSign
        //{
        //    get
        //    {
        //        //if (!string.IsNullOrEmpty(SignName))
        //        //    return true;
        //        return isSign;
        //    }
        //    set
        //    {
        //        isSign = value;
        //        //if (!isSign)
        //        //    SignName = null;
        //        this.NotifyPropertyChanged("IsSign");
        //    }
        //}
        private ObservableCollection<APIGroup> apiGroups;
        [DataMember(Name = "APIGroups")]
        //[XmlArrayItem("APIGroup")]
        public ObservableCollection<APIGroup> APIGroups
        {
            get
            {
                return apiGroups;
            }
            set
            {
                if (apiGroups != value)
                {
                    apiGroups = value;
                    this.NotifyPropertyChanged("APIGroups");
                }
            }
        }

        private SignType signType;
        [DataMember]
        public SignType SignType
        {
            get
            {
                return signType;
            }
            set
            {
                signType = value;
                this.NotifyPropertyChanged("SignType");
            }
        }

        [DataMember]
        public SignMethod SignMethod { get; set; }
    }

    public enum SignType
    {
        /// <summary>
        /// 不需要加密
        /// </summary>
        None,
        ///// <summary>
        ///// 所有参数按升序排序
        ///// </summary>
        //AllPramaAscending,
        ///// <summary>
        ///// 所有参数按降序排序
        ///// </summary>
        //AllPramaDescending,
        /// <summary>
        /// 自定义
        /// </summary>
        Customer,
        ///// <summary>
        ///// Oauth2
        ///// </summary>
        //Other
    }
    /// <summary>
    /// 目前支持MD5加密
    /// </summary>
    public enum SignMethod
    {
        MD5,
        SHac1,
        Other
    }

}
